<template>
  <el-card style="margin-bottom:20px;">
    <div slot="header" class="clearfix">
      <span>About me</span>
    </div>

    <div class="user-profile">
      <div class="box-center">
        <pan-thumb :image="user.avatar" :height="'100px'" :width="'100px'" :hoverable="false">
          <div>Hello </div>
          {{ user.role }}
        </pan-thumb>
      </div>
      <div class="box-center">
        <div class="user-name text-center">{{ user.name }}</div>
        <div class="user-role text-center text-muted">{{ user.role | uppercaseFirst }}</div>
      </div>
      <div>
        <span>姓名 : {{ userInfo.name }}</span><br>
        <span>性别 : {{ userInfo.gender }}</span><br>
        <span>电话号 : {{ userInfo.phone }}</span><br>
        <span>住址 : {{ userInfo.address }}</span><br>
        <span>生日 : {{ userInfo.birthday | formatDate }}</span><br>
        粉丝数 :<a href="/#" style="display: inline;" @click="getFans()">  {{ userInfo.fans }} </a>
        关注数 : <a href="/#" style="display: inline;" @click="getFollow()"> {{ userInfo.follow }} </a>
      </div>
      <div>
        <el-button type="text" size="large" @click="changeUserInfo">修改个人信息</el-button>
      </div>
      <div>
        <el-button type="text" size="large" @click="changeUserPass">修改密码</el-button>
      </div>
      <div />
      <div>
        <el-dialog title="我的关注" :visible.sync="followVisible" width="40%" :close-on-click-modal="false">
          <el-table :data="follow">
            <el-table-column label="头像">
              <template slot-scope="scope">
                <img :src="scope.row.photo" alt="用户头像" style="width: 50px; height: 50px;">
              </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="gender" label="性别" />
            <el-table-column prop="address" label="住址" />
            <el-table-column label="生日" prop="birthday">
              <template slot-scope="scope">
                {{ scope.row.birthday | formatDate }}
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
        <el-dialog title="我的粉丝" :visible.sync="fansVisible" width="40%" :close-on-click-modal="false">
          <el-table :data="fans">
            <el-table-column label="头像">
              <template slot-scope="scope">
                <img :src="scope.row.photo" alt="用户头像" style="width: 50px; height: 50px;">
              </template>
            </el-table-column>
            <el-table-column prop="name" label="姓名" />
            <el-table-column prop="gender" label="性别" />
            <el-table-column prop="address" label="住址" />
            <el-table-column label="生日" prop="birthday">
              <template slot-scope="scope">
                {{ scope.row.birthday | formatDate }}
              </template>
            </el-table-column>
          </el-table>
        </el-dialog>
        <el-dialog title="详细信息" :visible.sync="fromVisible" width="40%" :close-on-click-modal="false" destroy-on-close>
          <el-form :model="userInfo">
            <div style="text-align: center">
              <el-upload
                class="avatar-uploader"
                action="http://localhost:8081/common/fileUpload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img :src="displayedImage" class="avatar" style="border-radius: 75px; width: 150px">
              </el-upload>
            </div>
            <el-form-item label="姓名">
              <el-input v-model="userInfo.name" autocomplete="off" name="name" />
            </el-form-item>
            <el-form-item label="生日" prop="birthday">
              <el-date-picker
                v-model="userInfo.birthday"
              >
                type="date"
                value-format="yyyy-MM-dd"
                placeholder="选择日期">
              </el-date-picker>
            </el-form-item>
            <el-form-item label="性别">
              <el-radio v-model="userInfo.gender" label="男">男</el-radio>
              <el-radio v-model="userInfo.gender" label="女">女</el-radio>
            </el-form-item>
            <el-form-item label="地址">
              <el-row>
                <el-col :span="12">
                  <el-form-item label="省" prop="sheng">
                    <el-select v-model="sheng" placeholder="请选择省" @change="getCity">
                      <el-option v-for="item in province" :key="item.cid" :label="item.cname" :value="item.cid" />
                    </el-select>
                  </el-form-item>
                </el-col>
                <el-col :span="12">
                  <el-form-item label="市" prop="shi">
                    <el-select v-model="shi" placeholder="请选择市" @change="getPrefecture">
                      <el-option v-for="item in city" :key="item.cid" :label="item.cname" :value="item.cid" />
                    </el-select>
                  </el-form-item>
                </el-col>
              </el-row>
              <el-form-item label="区" prop="qu">
                <el-select v-model="qu" placeholder="请选择区">
                  <el-option v-for="item in prefecture" :key="item.cid" :label="item.cname" :value="item.cid" />
                </el-select>
              </el-form-item>
            </el-form-item>
            <el-form-item />
            <el-form-item>
              <el-button type="primary" @click="fromVisible = false">取消</el-button>
              <el-button type="primary" @click="onsubmit(userInfo)">确定</el-button>
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="fromVisible = false">确 定</el-button>
          </div>
        </el-dialog>
        <el-dialog title="修改密码" :visible.sync="passVisible" width="30%" :close-on-click-modal="false" destroy-on-close>
          <el-form ref="formRef" :model="user" label-width="80px" style="padding-right: 20px" :rules="rules">
            <el-form-item label="原始密码" prop="password">
              <el-input v-model="user.password" show-password placeholder="原始密码" />
            </el-form-item>
            <el-form-item label="新密码" prop="newPassword">
              <el-input v-model="user.newPassword" show-password placeholder="新密码" />
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input v-model="user.confirmPassword" show-password placeholder="确认密码" />
            </el-form-item>
          </el-form>
          <div slot="footer" class="dialog-footer">
            <el-button @click="passVisible = false">取 消</el-button>
            <el-button type="primary" @click="save(user)">确 定</el-button>
          </div>
        </el-dialog>
      </div>
    </div>

  </el-card>
</template>

<script>
import PanThumb from '@/components/PanThumb/index.vue'
import { getAction, postAction, putAction } from '@/utils/xhs-ajax'
export default {
  components: { PanThumb },
  props: {
    user: {
      type: Object,
      default: () => {
        return {
          name: '',
          email: '',
          avatar: '',
          role: ''
        }
      }
    }
  },
  data() {
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请确认密码'))
      } else if (value !== this.user.newPassword) {
        callback(new Error('确认密码错误'))
      } else {
        callback()
      }
    }
    return {
      userInfo: [],
      sheng: '',
      shi: '',
      qu: '',
      followVisible: false,
      fansVisible: false,
      fromVisible: false,
      passVisible: false,
      rules: {
        password: [
          { required: true, message: '请输入原始密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
        ],
        newPassword: [
          { required: true, message: '请输入新密码', trigger: 'blur' },
          { min: 6, max: 20, message: '长度在 6到 20 个' }
        ],
        confirmPassword: [
          { validator: validatePassword, required: true, trigger: 'blur' }
        ],
        sheng: [
          { required: true, message: '请选择省', trigger: 'change' }
        ],
        shi: [
          { required: true, message: '请选择市', trigger: 'change' }
        ],
        qu: [
          { required: true, message: '请选择区', trigger: 'change' }
        ]
      },
      province: [], // 省份
      city: [], // 城市
      prefecture: [], // 地区
      imgerUrl: '',
      fans: [],
      follow: []
    }
  },
  computed: {
    displayedImage() {
      return this.imageUrl || this.userInfo.photo
    }
  },
  created() {
    this.findUser()
  },
  methods: {
    formatDate() {
      return formatDate
    },
    getFans() {
      this.fansVisible = true
      getAction('/follow/checkUserFocus.do').then(res => {
        this.fans = res.data
      })
    },
    getFollow() {
      this.followVisible = true
      getAction('/follow/checkPassiveFocus.do').then(res => {
        this.follow = res.data
      })
    },
    findUser() {
      getAction('/user/checkUser.do').then(res => {
        this.userInfo = res.data
        getAction('/user/findAddress.do?address=' + this.userInfo.address).then(res => {
          this.sheng = res.data[0]
          this.shi = res.data[1]
          this.qu = res.data[2]
          this.getProvince()
          this.getCity(this.sheng)
          this.getPrefecture(this.shi)
        })
      })
    },
    changeUserInfo() {
      this.fromVisible = true
    },
    changeUserPass() {
      this.passVisible = true
    },
    onsubmit(userInfo) {
      console.log(userInfo)
      postAction('/user/updateUser.do?sheng=' + this.sheng + '&shi=' + this.shi + '&qu=' + this.qu, userInfo).then(res => {
        this.findUser()
        this.fromVisible = false
      })
    },
    save(user) {
      putAction('/user/updatePassword.do?oldPassword=' + user.password + '&newPassword=' + user.newPassword).then(res => {
        this.passVisible = false
      })
    },
    getProvince() { // 页面加载时，获取省份数据
      getAction(`/user/findCityByParentId.do?parentId=${1}`).then(res => {
        this.province = res.data
      })
    },
    getCity(data) { // 获取市数据
      getAction(`/user/findCityByParentId.do?parentId=${data}`).then(res => {
        this.city = res.data
        this.shi = this.city[0].cid
        this.getPrefecture(this.shi)
      })
    },
    getPrefecture(data) { // 获取地区数据
      getAction(`/user/findCityByParentId.do?parentId=${data}`).then(res => {
        this.prefecture = res.data
        this.qu = this.prefecture[0].cid
      })
    },
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw)
      this.userInfo.photo = this.imageUrl
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg'
      const isLt2M = file.size / 1024 / 1024 < 2

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!')
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!')
      }
      return isJPG && isLt2M
    }
  }
}
</script>

<style lang="scss" scoped>
a {
  color: #409EFF;
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}
.box-center {
  margin: 0 auto;
  display: table;
}

.text-muted {
  color: #777;
}

.user-profile {
  .user-name {
    font-weight: bold;
  }

  .box-center {
    padding-top: 10px;
  }

  .user-role {
    padding-top: 10px;
    font-weight: 400;
    font-size: 14px;
  }

  .box-social {
    padding-top: 30px;

    .el-table {
      border-top: 1px solid #dfe6ec;
    }
  }

  .user-follow {
    padding-top: 20px;
  }
}

.user-bio {
  margin-top: 20px;
  color: #606266;

  span {
    padding-left: 4px;
  }

  .user-bio-section {
    font-size: 14px;
    padding: 15px 0;

    .user-bio-section-header {
      border-bottom: 1px solid #dfe6ec;
      padding-bottom: 10px;
      margin-bottom: 10px;
      font-weight: bold;
    }
  }
}
</style>
